//关闭弹窗-layer	
//复选事件
/*给单选框或复选框添加样式*/
function checkbox($this){
    if($this.attr('type')=="checkbox"){
        if ($this.attr('checked')=="checked") {
            $this.removeAttr("checked");
            $this.next('img').attr("src","/static/images/c_checkbox_off.png");
        }else{
            $this.attr("checked","checked");
            $this.next('img').attr("src","/static/images/c_checkbox_on.png");
        }
    }
}
function fmtDate(obj){
    var date =  new Date(obj);
    var y = 1900+date.getYear();
    var m = "0"+(date.getMonth()+1);
    var d = "0"+date.getDate();
    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}
function change_time(obj){
    var date =  new Date(obj);
    var y = 1900+date.getYear();
    var m = "0"+(date.getMonth()+1);
    var d = "0"+date.getDate();
    return y+"-"+m.substring(m.length-2,m.length)+"-"+d.substring(d.length-2,d.length);
}
var allId = []
$(function(){
    //全选
    $('.SelectAllImg').click(function(){
        var $list_input=$(this).parents('.tbhead').next('.tbbody-user').find('input[type=checkbox]'); //找到子类的按钮
        var idAll =[];
        if ($(this).attr("checked")==undefined || $(this).attr("checked")=="") {
            $list_input.attr("checked","checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_on.png");
            $(".tbbody-user input[type='checkbox']").each(function() {
                var id = $(this).attr('data-id')
                idAll.push(id);
                allId=idAll
            })
            console.log(1)
        }else{
            $list_input.removeAttr("checked");
            $list_input.next('img').attr("src","/static/images/c_checkbox_off.png");
            allId=[];
            console.log(2)
        }
        checkbox($(this));
    })
    //单选
    $("body").on("change",".tbbody-user input[type='checkbox']",function(){

        var $ul_input=$(this).parents('.tbbody-user').prev('.tbhead').find('input');
        if($(this).parents('.tbhead').find("input[checked='checked']").length== $('.checkbox_child').length){
            $ul_input.prop("checked","checked");
            $ul_input.next('img').attr("src","/static/images/c_checkbox_on.png");
        }else{
            $ul_input.prop("checked",false);
            $ul_input.next('img').attr("src","/static/images/c_checkbox_off.png");
        }

        if($(this).attr("checked")=="" || $(this).attr("checked")==undefined){
            $(this).attr("checked",true);
            $(this).next('img').attr("src","/static/images/c_checkbox_on.png");
            var idAll =[];
            $(".tbbody-user input[type='checkbox']:checked").each(function(){
                var id = $(this).attr("data-id");
                console.log('正选')
                idAll.push(id);
                allId=idAll
            })

            if(allId.length==1){
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/aptitude/license/license_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        var data  = data.license_info
                        console.log(data)
                        $(".c-cusname").find("option:selected").text(data.cust_name);
                        $(".license_license").attr('value',data.license)
                        $(".license_credit_code").attr('value',data.credit_code)
                        $(".c-cusnames").find("option:selected").text(data.cust_name);
                        $(".license_licenses").attr('value',data.license)
                        $(".license_credit_codes").attr('value',data.credit_code)
                        if(data.license_validity==1){
                            $(".s_beginTime").attr('value','永久')
                            $(".s_beginTimes").attr('value','永久')
                        }else{
                            $(".s_beginTime").attr('value',fmtDate(data.license_validity*1000))
                            $(".s_beginTimes").attr('value',fmtDate(data.license_validity*1000))
                        }

                    }
                });
            }



        }else{
            $(this).attr("checked",false);
            $(this).next('img').attr("src","/static/images/c_checkbox_off.png");
            var id = $(this).attr("data-id");
            var index =  allId.indexOf(id);
            console.log(index)
            if(index>-1){
                allId.splice(index, 1);
            }else{
                allId=[]
            }
            console.log(allId)
            if(allId.length==1) {
                var userid  = allId['0']
                console.log(userid)
                $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                $.ajax({
                    type: "POST",
                    url: "/aptitude/license/license_info",
                    dataType:'json',
                    data: {userid:userid},
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data){
                        var data  = data.license_info
                        $(".c-cusname").find("option:selected").text(data.cust_name);
                        $(".license_license").attr('value',data.license)
                        $(".license_credit_code").attr('value',data.credit_code)
                        $(".c-cusnames").find("option:selected").text(data.cust_name);
                        $(".license_licenses").attr('value',data.license)
                        $(".license_credit_codes").attr('value',data.credit_code)
                        if(data.license_validity==1){
                            $(".s_beginTime").attr('value','永久')
                            $(".s_beginTimes").attr('value','永久')
                        }else{
                            $(".s_beginTime").attr('value',fmtDate(data.license_validity*1000))
                            $(".s_beginTimes").attr('value',fmtDate(data.license_validity*1000))
                        }

                    }
                });
            }
        }
    })
})

//顶部导航菜单js
$(function(){

//    资料变更
    $(".btn-danger").on('click',function(){
        if(allId.length==0){
            layer.msg('请选择您要变更的营业执照',{icon:2,time:1500});
            return;
        }else if(allId.length>1){
            layer.msg('一次只能变更一个营业执照',{icon:2,time:1500});
            return;
        }else{
            var userid = allId['0']
            layer.open({
                type:1,
                title: '营业执照变更',
                area:['695px',''],
                btn:["确定","取消"],
                shadeClose:true,
                content:$('.j-buessChangeTan'),
                yes:function(){
                    var reg = /^[0-9A-Z]{18}$/ ; //信用代码验证
                    var creditma = $.trim($(".c-creditma").val())
                    var time = $.trim($(".c-daima").val())
                    var cusname = $(".c-cusname").find("option:selected").val();
                    var buess_file = $.trim($(".j-buessuper").val())

                    //
                    if(!buess_file){
                        $(".j-buessuper").focus();
                        layer.msg('上传资料内容不能为空!',{icon:2,time:1500});
                        return ;
                    }else{

                    }
                    //信用代码验证
                    if(!creditma){
                        $(".c-creditma").focus();
                        layer.msg('信用代码不能为空!',{icon:2,time:1500});
                        return ;
                    }else if(!reg.test(creditma)){
                        $(".c-creditma").focus();
                        layer.msg('请输入正确的信用代码!',{icon:2,time:1500});
                        return ;
                    }else{

                    }

                    console.info(cusname)
                    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                    $.ajax({
                        type: "POST",
                        url: "/aptitude/license/license_update",
                        dataType:'json',
                        data: {buess_file:buess_file,creditma:creditma,time:time,cusname:cusname,userid:userid},
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function(data){
                        console.log(data)
                            if(data.status==1){
                                layer.msg(data.msg,{icon:1,time:1500});
                                setTimeout(function(){
                                    location.href="/aptitude/license/license"
                                },1500)
                            }else if(data.status==2){
                            console.log('111')
                                layer.msg(data.msg,{icon:2,time:1500});
                                setTimeout(function(){
                                    location.href="/auth/login"
                                },1500)

                            }else{
                                layer.msg(data.msg,{icon:2,time:1500});
                            }

                        }
                    });
                }
            });
        }
    })

    $(".btn-jiucuo").on('click',function(){
        if(allId.length==0){
            layer.msg('请选择您要纠错的营业执照',{icon:2,time:1500});
            return;
        }else if(allId.length>1){
            layer.msg('一次只能纠错一个营业执照',{icon:2,time:1500});
            return;
        }else{
            var userid = allId['0']
            layer.open({
                type:1,
                title: '营业执照纠错',
                area:['695px',''],
                btn:["确定","取消"],
                shadeClose:true,
                content:$('.j-buessChangeJiu'),
                yes:function(){
                    var reg = /^[0-9A-Z]{18}$/ ; //信用代码验证
                    var creditma = $.trim($(".license_credit_codes").val())
                    var time = $.trim($(".s_beginTimes").val())
                    var cusname = $(".c-cusnames").find("option:selected").val();

                    //信用代码验证
                    if(!creditma){
                        $(".license_credit_codes").focus();
                        layer.msg('信用代码不能为空!',{icon:2,time:1500});
                        return ;
                    }else if(!reg.test(creditma)){
                        $(".license_credit_codes").focus();
                        layer.msg('请输入正确的信用代码!',{icon:2,time:1500});
                        return ;
                    }else{
                        console.info(creditma)
                    }

                    console.info(cusname)
                    $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
                    $.ajax({
                        type: "POST",
                        url: "/aptitude/license/correction",
                        dataType:'json',
                        data: {creditma:creditma,time:time,cusname:cusname,userid:userid},
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function(data){

                            if(data.status==1){
                                layer.msg(data.msg,{icon:1,time:1500});
                                setTimeout(function(){
                                    location.href="/aptitude/license/license"
                                },1500)
                            }else if(data.status==2){
                                console.log('111')
                                layer.msg(data.msg,{icon:2,time:1500});
                                setTimeout(function(){
                                    location.href="/auth/login"
                                },1500)

                            }else{
                                layer.msg(data.msg,{icon:2,time:1500});
                            }


                        }
                    });
                }
            });
        }
    })

    $("#sample_bussDown").on('click',function(){

        console.log(allId)
        if(allId.length==0||allId==''){

            layer.msg('请选择您要下载的营业执照',{icon:2,time:1500});
            return;
        }
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/aptitude/license/download",
            dataType:'json',
            data: {allId:allId},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function(data){

                console.log(data)

                var mp3arr = data.url;
                var namearr = data.name
                console.log(mp3arr)


                function download(name, href) {
                    var a = document.createElement("a"), //创建a标签
                        e = document.createEvent("MouseEvents"); //创建鼠标事件对象
                    e.initEvent("click", false, false); //初始化事件对象
                    a.href = href; //设置下载地址
                    a.download = name; //设置下载文件名
                    a.dispatchEvent(e); //给指定的元素，执行事件click事件
                }

                //给多文件下载按钮添加点击事件

                    for (var index = 0; index < mp3arr.length; index++) {
                        download(namearr[index], mp3arr[index]);

                }

                }

        });

    })
})

//

$(function(){
	//历史记录-弹窗
	$('#sample_bussHist').on('click', function(){
		layer.open({
		  	type:1,
			title: '历史记录',
			area:['800px',''],
			btn:["确定","取消"],
			shadeClose:true,
			offset: '10px', //只设置顶部
			content:$('.j-bussHistModel'),			
		});
	});

    $("#j-uploader input[type='file']").change(function(){
        $(this).parents().find(".j-buessuper").val($(this).val())
    })
    //变更


})

//筛选时间
$(function(){
    $(".cust_id input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
           var cust_id = $(this).val();
           console.log(cust_id)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/aptitude/license/cust_id_screen",
                dataType: 'json',
                data: {cust_id: cust_id},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {
                    var  license_validity=''
                    if(data.status==1){
                       var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();
                        for (var i = 0; i < dataList.length; i++) {
                            var str = "";
                            if(dataList[i].license_validity==1){
                                license_validity = '永久'
                            }else{
                                license_validity =  fmtDate(dataList[i].license_validity*1000)
                            }

                            str = '<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+i+'</td><td class="buss_td">'+dataList[i].cust_id+'</td><td class="buss_td">'+dataList[i].cust_name+'</td><td class="buss_td">'+dataList[i].credit_code+'</td><td class="buss_td">'+dataList[i].legal_name+'</td><td class="buss_td">'+license_validity+'</td><td class="buss_td"><p class="c-put orderedt" href="javascript:;"><img src="/upload/material/'+dataList[i].license+'"></p></td></tr>'
                            tr.append(str);
                            console.log(str)
                        }




                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })

    $("#search_cust").change(function(){
        var cust_name =$(this).find('option:selected').val();
        console.log(cust_name)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/aptitude/license/cust_name_screen",
            dataType: 'json',
            data: {cust_name: cust_name},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {
                var  license_validity=''
                if(data.status==1){
                    var dataList = data.dataList.data;
                    layer.msg(data.msg,{icon:1,time:1500});

                    console.log(dataList.length)
                    console.log(dataList)
                    var tr = $(".tbbody-user").empty();
                    for (var i = 0; i < dataList.length; i++) {
                        var str = "";
                        if(dataList[i].license_validity==1){
                            license_validity = '永久'
                        }else{
                            license_validity =  fmtDate(dataList[i].license_validity*1000)
                        }

                        str = '<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+i+'</td><td class="buss_td">'+dataList[i].cust_id+'</td><td class="buss_td">'+dataList[i].cust_name+'</td><td class="buss_td">'+dataList[i].credit_code+'</td><td class="buss_td">'+dataList[i].legal_name+'</td><td class="buss_td">'+license_validity+'</td><td class="buss_td"><p class="c-put orderedt" href="javascript:;"><img src="/upload/material/'+dataList[i].license+'"></p></td></tr>'
                        tr.append(str);
                        console.log(str)
                    }




                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })

    $(".credit_code input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var credit_code = $(this).val();
            console.log(credit_code)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/aptitude/license/credit_code_screen",
                dataType: 'json',
                data: {credit_code: credit_code},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {
                    var  license_validity=''
                    if(data.status==1){
                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();
                        for (var i = 0; i < dataList.length; i++) {
                            var str = "";
                            if(dataList[i].license_validity==1){
                                license_validity = '永久'
                            }else{
                                license_validity =  fmtDate(dataList[i].license_validity*1000)
                            }

                            str = '<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+i+'</td><td class="buss_td">'+dataList[i].cust_id+'</td><td class="buss_td">'+dataList[i].cust_name+'</td><td class="buss_td">'+dataList[i].credit_code+'</td><td class="buss_td">'+dataList[i].legal_name+'</td><td class="buss_td">'+license_validity+'</td><td class="buss_td"><p class="c-put orderedt" href="javascript:;"><img src="/upload/material/'+dataList[i].license+'"></p></td></tr>'
                            tr.append(str);
                            console.log(str)
                        }




                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })

    $(".legal_name input").bind('keypress',function (event) {
        if(event.keyCode == "13")

        {
            var legal_name = $(this).val();
            console.log(legal_name)
            $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
            $.ajax({
                type: "POST",
                url: "/aptitude/license/legal_name_screen",
                dataType: 'json',
                data: {legal_name: legal_name},
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                },
                success: function (data) {
                    var  license_validity=''
                    if(data.status==1){
                        var dataList = data.dataList.data;
                        layer.msg(data.msg,{icon:1,time:1500});

                        console.log(dataList.length)
                        console.log(dataList)
                        var tr = $(".tbbody-user").empty();
                        for (var i = 0; i < dataList.length; i++) {
                            var str = "";
                            if(dataList[i].license_validity==1){
                                license_validity = '永久'
                            }else{
                                license_validity =  fmtDate(dataList[i].license_validity*1000)
                            }

                            str = '<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+i+'</td><td class="buss_td">'+dataList[i].cust_id+'</td><td class="buss_td">'+dataList[i].cust_name+'</td><td class="buss_td">'+dataList[i].credit_code+'</td><td class="buss_td">'+dataList[i].legal_name+'</td><td class="buss_td">'+license_validity+'</td><td class="buss_td"><p class="c-put orderedt" href="javascript:;"><img src="/upload/material/'+dataList[i].license+'"></p></td></tr>'
                            tr.append(str);
                            console.log(str)
                        }




                    }else{
                        layer.msg(data.msg,{icon:2,time:1500});
                    }

                }

            });
        }

    })
    $(".time_screen input").change(function () {
        var beginTime =  $("#beginTime").val()
        var endTime =  $("#endTime").val()
        console.log(beginTime)
        console.log(endTime)
        $.ajaxSetup({headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}});
        $.ajax({
            type: "POST",
            url: "/aptitude/license/time_screen",
            dataType: 'json',
            data: {beginTime: beginTime,endTime:endTime},
            headers: {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            },
            success: function (data) {
                var  license_validity=''
                if(data.status==1){
                    var dataList = data.dataList.data;
                    layer.msg(data.msg,{icon:1,time:1500});

                    console.log(dataList.length)
                    console.log(dataList)
                    var tr = $(".tbbody-user").empty();
                    for (var i = 0; i < dataList.length; i++) {
                        var str = "";
                        if(dataList[i].license_validity==1){
                            license_validity = '永久'
                        }else{
                            license_validity =  fmtDate(dataList[i].license_validity*1000)
                        }

                        str = '<tr><td class="buss_td"><label><input type="checkbox" data-id="'+dataList[i].id+'" class="checkson checkbox_child"/><img src="/static/images/c_checkbox_off.png"/></label></td><td class="buss_td">'+i+'</td><td class="buss_td">'+dataList[i].cust_id+'</td><td class="buss_td">'+dataList[i].cust_name+'</td><td class="buss_td">'+dataList[i].credit_code+'</td><td class="buss_td">'+dataList[i].legal_name+'</td><td class="buss_td">'+license_validity+'</td><td class="buss_td"><p class="c-put orderedt" href="javascript:;"><img src="/upload/material/'+dataList[i].license+'"></p></td></tr>'
                        tr.append(str);
                        console.log(str)
                    }




                }else{
                    layer.msg(data.msg,{icon:2,time:1500});
                }

            }

        });

    })
})
$("#ImportPicInput").fileinput({
    uploadUrl: "/aptitude/license/upload",
    uploadAsync: true,
    showUpload:true,
    browseClass: "btn file-primary",
    // allowedFileExtensions: ['jpg', 'png', 'gif'],
    // maxFileSize
    uploadExtraData: function(previewId, index) {
        return {cust_id:allId['0']};
    },
    showPreview: false,
    showCaption: false,
    maxFileCount: 1
}).on('fileuploaded', function(e, params) {
    console.log(params)
    if(params.response.status==1){
        layer.msg(params.response.msg,{icon:1,time:1500});
    }else{
        layer.msg(params.response.msg,{icon:2,time:1500});
    }

});
